import pandas as pd
from sqlalchemy import create_engine

# MySQL数据库连接信息
db_host = 'localhost'
db_user = 'root'
db_password = '123456'
db_name = 'test'
table_name='funds'
def write_df_to_mysql(df):
    """
    将数据框写入MySQL数据库中的表
    """
    # 创建MySQL连接
    engine = create_engine(f'mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}')

    # chunksize = 1000
    # for i in range(0, len(df), chunksize):
    #     chunk = df.iloc[i:i + chunksize]
    #     try:
    #         chunk.to_sql(table_name, con=engine, if_exists='replace', index=False)
    #     except Exception as e:
    #         print(f"Error writing rows {i}-{i + chunksize}: {e}")








    # 将数据框写入MySQL数据库中的表
    df.to_sql(table_name, con=engine, if_exists='replace', index=False)

    # 关闭连接
    engine.dispose()
